Ext.define('Service.prop.controller.PropController',
		{
			extend : 'WebOs.ex.DefaultController',

			views : [ 'Service.prop.view.InitContainer',
					'Service.prop.view.PropGrid',
					'Service.prop.view.PropUserGrid',
					'Service.prop.view.PropAddWindow',
					'Service.prop.view.PropChgWindow',
					'Service.prop.view.PropUserAddWindow',
					'Service.prop.view.PropUserChgWindow'
					],

			stores : [ 'Service.prop.store.HtPropStore',
					'Service.prop.store.HtPropUserStore'

			],

			models : [ 'Service.prop.model.HtPropModel',
					'Service.prop.model.HtPropUserModel'

			],

			refs : [
			// getter setter
			],
			
			pid : null,
			

			init : function() {
				var me = this;
				this.control({
					'button[action=addHtProp]' : {
						click : me.onAddPropBtnClickFire
					},
					'button[id=Service_prop_view_PropSaveBtn]' : {
						click : me.onSavePropBtnClickFire
					},
					'button[id=Service_prop_view_PropCancelBtn]' : {
						click : me.onCancelPropBtnClickFire
					},
					'button[action=chgHtProp]' : {
						click : me.onChgPropBtnClickFire
					},
					'button[id=Service_prop_view_PropChgBtn]' : {
						click : me.onPropChgBtnClickFire
					},
					'button[id=Service_prop_view_PropChgCancelBtn]' : {
						click : me.onPropChgCancelBtnClickFire
					},
					'button[action=delHtProps]': {
						click : me.onDelPropBtnClickFire
					},
					'button[action=addHtPropUser]' : {
						click : me.onAddPorpUserClickFire
					},
					'button[id=Service_prop_view_PropUserSaveBtn]' : {
						click : me.onUserSaveBtnClickFire
					},
					'button[id=Service_prop_view_PropUserCancelBtn]': {
						click : me.onUserCancelBtnClickFire
					},
					'button[action=chgHtPropUser]' : {
						click : me.onChgPropUserBtnClickFire
					},
					'gridpanel[id=Service_prop_view_PropUserGrid]' : {
						afterrender : me.onPropUserGridRenderFire
					},
					'gridpanel[id=Service_prop_view_PropGrid]' : {
						render : me.onPropGridAfterRenderFire,
						itemclick : me.onPropGridItemClickFire
					},
					'button[id=Service_prop_view_PropUserChgSaveBtn]' : {
						click : me.onUserChgSaveBtnClickFire
					},
					'button[id=Service_prop_view_PropUserChgCancelBtn]' : {
						click : me.onUserChgCancelBtnClickFire
					},
					'button[action=delHtPropUsers]' : {
						click : me.onDelPropUserBtnClickFire
					}
					
				});
			},
			
			onDelPropUserBtnClickFire : function(cmp){
				var me = this;
				var sel = Ext.getCmp('Service_prop_view_PropUserGrid').getSelectionModel().getSelection();
				if(sel.length <1){
					Ext.Msg.alert('提示','请至少选中一个人员');
				}else{
					var ids = [];
					for (var p in sel){
						ids.push(sel[p].get('id'));
					}
					if(ids.length > 0){
						me.request({
							params : {
								module : 'Service_prop',
								action : 'delHtPropUsers',
								ids : Ext.encode(ids)
							},
							onSuccess : function(rep){
								if(me.pid){
									me.queryPropUsersByPid(me.pid);
								}else{
									Ext.getCmp('Service_prop_view_PropUserGrid').getStore().load();
								}
							}
						});
					}
				}
			},
			onUserChgSaveBtnClickFire : function(cmp){
				var me = this;
				var form = cmp.up('window').down('form').getForm();
				var values = form.getValues();
				me.request({
					params : Ext.apply(values,{
						module : 'Service_prop',
						action : 'chgHtPropUser'
					}),
					onSuccess : function(){
						if(me.pid){
							me.queryPropUsersByPid(me.pid);
						}else{
							Ext.getCmp('Service_prop_view_PropUserGrid').getStore().load();
						}
						cmp.up('window').destroy();
					}
				});
				
			},
			
			onUserChgCancelBtnClickFire : function(cmp){
				cmp.up('window').destroy();
			},
			
			onChgPropUserBtnClickFire : function(cmp){
				var sel = Ext.getCmp('Service_prop_view_PropUserGrid').getSelectionModel().getSelection();
				if(sel.length == 1){
					var chgWin = Ext.create('Service.prop.view.PropUserChgWindow').show();
					chgWin.down('form').loadRecord(sel[0]);
					
				}else{
					Ext.Msg.alert('提示','请选中一个人员');
				}
			},
			
			onPropUserGridRenderFire : function(cmp){
				cmp.getStore().load();
			},
			onPropGridItemClickFire : function(cmp , record){
				var me = this;
				var pid = record.get('id');
				me.pid = pid;
				me.queryPropUsersByPid(pid);
			},
			
			queryPropUsersByPid : function(pid){
				var me = this;
				if(pid){
					me.request({
						params : {
							module : 'Service_prop',
							action : 'getHtPropUsersByPropId',
							propId : pid
						},
						onSuccess : function(p,resp){
							var datas = p.result;
							Ext.getCmp('Service_prop_view_PropUserGrid').getStore().loadData(datas);
						}
					});
				}
			},
			
			
			onUserCancelBtnClickFire : function(cmp){
				cmp.up('window').destry();
			},
			onPropChgCancelBtnClickFire : function(cmp){
				cmp.up('window').destroy();
			},
			onUserSaveBtnClickFire : function(cmp){
				var me = this;
				var form = cmp.up('window').down('form').getForm();
				var values = form.getValues();
				console.log(values);
				if(form.isValid()){
					me.request({
						params : Ext.apply(values,{
							module : 'Service_prop',
							action : 'addHtPropUser'
						}),
						onSuccess : function(){
							cmp.up('window').destroy();
							Ext.getCmp('Service_prop_view_PropUserGrid').getStore().load();
							Ext.getCmp('Service_prop_view_PropGrid').getStore().load();
						}
					});
				}
			},
			onDelPropBtnClickFire : function(){
				var me = this;
				var sel = Ext.getCmp('Service_prop_view_PropGrid').getSelectionModel().getSelection();
				if(sel.length <1 ){
					Ext.Msg.alert('提示','没有选中物业');
				}else{
					var ids = [];
					for(var p in sel){
						ids.push(sel[p].get('id'));
					}
					Ext.MessageBox.confirm('操作确认', '确定要删除这'+ids.length+'项吗?', function(btn) {
						if (btn == 'yes') {
							me.request({
								params : {
									ids : Ext.encode(ids),
									module : 'Service_prop',
									action : 'delHtProps'
								},
								onSuccess : function(){
									Ext.Msg.alert('提示','删除成功');
									Ext.getCmp('Service_prop_view_PropGrid').getStore().load();
								}
							});
						}
					}, this);
					
				}
			},
			
			onAddPorpUserClickFire : function(cmp){
				var sel = Ext.getCmp('Service_prop_view_PropGrid').getSelectionModel().getSelection();
				if(sel.length != 1 ){
					Ext.Msg.alert('提示','请选中一个物业');
				}else{
					var propId = sel[0].get('id');
					if(propId){
						Ext.create('Service.prop.view.PropUserAddWindow',{'propId': propId}).show();
					}
				}
				
			},
			
			onPropChgBtnClickFire : function(cmp){
				var me = this;
				var form = cmp.up('window').down('form').getForm();
				if(form.isValid()){
					var param = form.getValues();
					me.request({
						params : Ext.apply(param,{
							module : 'Service_prop',
							action : 'chgHtProp'
						}),
						onSuccess : function(){
							cmp.up('window').destroy();
							Ext.getCmp('Service_prop_view_PropGrid').getStore().load();
						}
					})
				}
			},

			onAddPropBtnClickFire : function(cmp) {
				Ext.create('Service.prop.view.PropAddWindow').show();
			},
			onChgPropBtnClickFire : function(){
				var sel = Ext.getCmp('Service_prop_view_PropGrid').getSelectionModel().getSelection();
				if(sel.length>1 || sel.length==0){
					Ext.Msg.alert('错误','请选择一个物业进行修改');
				}else{
					Ext.create('Service.prop.view.PropChgWindow',{
						name : sel[0].get('name'),
						propId : sel[0].get('id')
					}).show();
				}
			},
			

			onSavePropBtnClickFire : function(cmp) {
				var me = this;
				var form = cmp.up('window').down('form').getForm();
				if(form.isValid()){
					Ext.Ajax.request({
						url : Constants.REQUEST_URL,
						method : 'POST',
						params : {
							module : 'Service_prop',
							action : 'addHtProp',
							name : form.getValues()
						},
						success : function(){
							cmp.up('window').destroy();
							Ext.getCmp('Service_prop_view_PropGrid').getStore().load();
						}
					});
				}
			},

			onCancelPropBtnClickFire : function(cmp) {
				cmp.up('window').destroy();
			},
			
			onPropGridAfterRenderFire : function(cmp){
				cmp.getStore().load();
			}

		});
